package com.example.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import com.example.R;
import com.example.common.GlobalConstant;
import com.example.common.GlobalVariable;
import com.example.modle.Mp3Info;
import com.example.util.FileUtil;
import org.apache.http.auth.NTCredentials;

import java.io.File;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: Jasic
 * Date: 12-11-
 *
 * 本地的mp3资源
 */
public class LocalResourceActivity extends AResourceActivity {

    private String localUrl = GlobalVariable.MP3_ROOT;

    private List<Mp3Info> mp3InfoList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.localresourcelayout);
        super.updateListView();

    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {

        Mp3Info mp3 = this.mp3InfoList.get(position);
        Intent intent = new Intent(this, PlayerActivity.class);
        intent.putExtra("mp3", mp3);
        super.startActivity(intent);

        super.onListItemClick(l, v, position, id);
    }

    public List<Mp3Info> getMp3Infos() {

        File root = new File(localUrl);
        List<File> mp3Files = FileUtil.filter(root, GlobalConstant.MP3_SUFIXX);
        List<File> lrcFiles = FileUtil.filter(root, GlobalConstant.LRC_SUFIXX);

        Map<String, Mp3Info> mp3Infos = new HashMap<String, Mp3Info>(mp3Files.size());
        for (File file : mp3Files) {

            Mp3Info info = new Mp3Info();
            info.setMp3_Name(file.getName());
            info.setMp3_Size(file.length() + "");
            mp3Infos.put(file.getAbsolutePath().split(GlobalConstant.MP3_SUFIXX)[0], info);

        }

        for (File file : lrcFiles) {

            Mp3Info mp3Info = mp3Infos.get(file.getAbsolutePath().split(GlobalConstant.LRC_SUFIXX)[0]);
            if (null != mp3Info) {
                mp3Info.setLrc_Name(file.getName());
                mp3Info.setLrc_Size(file.length() + "");
            }
        }

        this.mp3InfoList =  new ArrayList<Mp3Info>(mp3Infos.values());
        return this.mp3InfoList;
    }
}
